<?php
namespace App\Http\Controllers\Admin;
use App\Models\Admin\TvmMobileDetail;
use App\Models\Admin\TvmMobileCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\AdminAction;


class AdminController extends CommonController {

	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}


	/*
     * 管理员列表
     */
    public function adminList(Request $request){
    	$condition = array();
		$keywords = $request->input('keywords');
		if($keywords){
			$condition[] = ['username', 'like', '%'.$keywords.'%'];
		}


        $list = DB::table('tpc_admin t')
        	->where($condition)
            ->leftJoin('auth_group a','t.admin_group_id' , '=' , 'a.id')
            ->select('a.title','t.username','t.id')
            ->paginate(10);

		return view('admin.admin.admin_list',[
			'data' => $list,
			'keywords' => $keywords
		]);
    }

    /*
     * 添加权限菜单
     */
    public function adminAdd(Request $request){
    	
    	$id = $request->input('id');
    	if($request->isMethod('post')){
    		if(!empty($id)){
                $where[] = ['id','!=',$id];
            }
           	$gid = $request->input('gid');
			if($gid<=0){
				$this->ajax_error('请选择用户组');
			}
			$username = $request->input('username');
			if(!$username){
				$this->ajax_error('用户名不能为空');
			}
			$where[] = ['username','=',$username];

			$userInfo = DB::table('tpc_admin')->where($where)->get();
			if(!$userInfo->isEmpty()){
				$this->ajax_error('用户名已存在');
			}
			$data = array(
				'id' => TvmMobileCategory::getNextSeq(),
				'admin_group_id' => $request->input('gid'), //父级ID
				'username' => $request->input('username') //用户名称
				
			);
			$password = $request->input('password'); //用户密码
			if(empty($id)){
                $data['pwd'] = md5($password);
               // var_dump($data);exit;
                Db::table('tpc_admin')->insert($data);
                $adminArr = DB::table('tpc_admin')->where('id',$data['id'])->first();
                $admin_id =  $adminArr->id;
            }else{

                if(!empty($password)){
                    $data['password'] = md5($password);
                }
                $data['id'] = $id;
                $bool = DB::table('tpc_admin')->where('id',$id)->update($data);
                $adminArr = DB::table('tpc_admin')->where('id',$data['id'])->first();
                $admin_id =  $adminArr->id;
            }
            if($admin_id){
                if(empty($id)){
                    $insert['mid'] = $admin_id;
                    $insert['group_id'] = $data['admin_group_id'];
                    $result = Db::table('auth_group_access')->insert($insert);
                }else{
                    $insert['group_id'] = $data['admin_group_id'];
                    $result = DB::table('auth_group_access')->where('mid',$id)->update($insert);
                }
                $this->ajax_success('添加成功');
            }else{
                $this->ajax_error('添加失败');
            }


		
		}

		$condition = array();
		if($id){
			$condition[] = ['id','=',$id];
		}else{
			$condition[] = ['id','=',''];
		}

		$category = Db::table('auth_group')->orderBy('id','desc')->get();

		//$category = DB::table('auth_group')->where($condition)->select('*')->get();
		$list =  Db::table('tpc_admin')->where($condition)->first();
		return view('admin.admin.admin_add',[
			'category' => $category,
			'data' => $list,
			'id' => $id,
			'action' => 'Add'
		]);

    }


	/**
	 * @param Request $request
	 * 删除管理员数据
	 */
	public function adminDel(Request $request){
		$aid = $request->input('id');
		 if(!empty($aid)){
            $idData = explode(',',$aid);
            foreach($idData as $k=>$v){
               $result = DB::table('tpc_admin')->where('id',$v)->delete();
            }
           $this->ajax_success('删除成功',$result);
        } else {
            $this->ajax_error('删除失败',$result);
        }
	}


}